<template>
	<view class="">
		<u-upload :file-list="fileList" @afterRead="afterRead" @delete="deletePic" max-count="1" :multiple="false" :preview-full-image="false">
		</u-upload>
		<view  class="el-upload__tip">{{datas.tip}}</view>
	</view>
</template>

<script>
	import {
		URL_IP_HOST,
		URL_PREFIX
	} from '@/util/constant.js'
	export default {
		props: ["data"],
		data() {
			var userInfoData_ = uni.getStorageSync("userInfoData_")
			return {
				fileList: [],
				imageUrl: '',
				datas: {},
				headers: {
					"Authorization": `Bearer ${userInfoData_ ? userInfoData_.access_token : ""}`,
					"clientid": userInfoData_ ? userInfoData_.client_id : "eeb9377d4b6debbc0a6e8756bbbbb4c7"
				},
				action: URL_IP_HOST + "/application/oss/upload"
			};
		},
		watch: {
			data: {
				handler: function(newValue) {
					console.log("监听data", newValue);
					this.fileList = []
					if (newValue.url) {
						this.fileList.push({
							url: newValue.url
						})
					}
					this.datas = this.data
					// this.imageUrl = this.datas.url
				},
				immediate: true,
				deep: true,
			},
		},
		created() {
			console.log("地址", this.action);
		},
		methods: {
			deletePic(e) {
				console.log(e);
				this[`fileList${e.name}`].splice(e.index, 1)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					// console.log(JSON.parse(result.data));
					let obj=JSON.parse(result.data).data
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						fileName:obj.fileName,
						url: obj.url,
						ossId:obj.ossId
					}))
					fileListLen++
				}
				console.log("上传成功：",this.fileList);
					this.$emit('uploadImageClick', this.fileList[0])
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.action, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							
							setTimeout(() => {
								resolve(res)
							}, 1000)
						}
					});
				})
			},
			handleAvatarSuccess(lists, name) {
				console.log("图片上传成功", lists, name);
				// this.imageUrl = URL.createObjectURL(file.raw);
				// console.log("图片上传成功：",res);
				// this.$emit('uploadImageClick', res.data)
			},
			beforeAvatarUpload(index, list) {
				console.log("图片上传格式：", index, list);
				return true
				if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/png') {
					this.$message({
						message: '图片格式有误! 仅支持jpg、jpeg、png格式!',
						type: 'error',
						center: true,
						offset: 0.1
					});
					return false
				} else if (rawFile.size / 1024 / 1024 > 2) {
					this.$message({
						message: '图片大小不超过2MB!',
						type: 'error',
						center: true,
						offset: 0.1
					});
					return false
				}
				// return isJPG && isLt2M;
			}
		}
	}
</script>
<style lang="scss">
	::v-deep .avatar-uploader {
		.el-upload {
			border: 1px dashed #d9d9d9;
			border-radius: 6px;
			cursor: pointer;
			position: relative;
			overflow: hidden;
		}
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 90px;
		height: 90px;
		line-height: 90px;
		text-align: center;
	}

	.avatar {
		width: 90px;
		height: 90px;
		display: block;
	}
</style>
